﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Model;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace BLL.reccommendVideo
{

    public class reccommendVideoManager
    {
        public static void ChangeFlashXML()
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            List<Model.Video> videoList = videoDAL.GetVideosIsTry();
            int length = videoList.Count();
            Console.WriteLine("manager = " + length);
            Model.Video[] videArray = videoList.ToArray();
            Model.Video[] videArray1 = QuickSort(videArray, 0, length - 1);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("./global/homePage/dl_sample6.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("directLinks").ChildNodes;//获取Employees节点的所有子节点 
            Table table = new Table();

            nodeList[1].InnerText = videArray1.ElementAt(0).VideoImage;
            //nodeList[2].InnerText = "../movies/sample1.swf";
            nodeList[2].InnerText = "./movieDetailedInfo.aspx?videoId=" + videArray1.ElementAt(0).VideoId;

            nodeList[13].InnerText = videArray1.ElementAt(1).VideoImage;
            //nodeList[14].InnerText = "videoLib.aspx?videoId"+videoList.ElementAt(1).VideoId;
            nodeList[14].InnerText = "./movieDetailedInfo.aspx?videoId=" + videArray1.ElementAt(1).VideoId;

            nodeList[25].InnerText = videArray1.ElementAt(2).VideoImage;
           // nodeList[26].InnerText = "videoLib.aspx?videoId ="+videoList.ElementAt(2).VideoId;
            nodeList[26].InnerText = "./movieDetailedInfo.aspx?videoId=" + videArray1.ElementAt(2).VideoId;

            nodeList[37].InnerText = videArray1.ElementAt(3).VideoImage;
            //nodeList[38].InnerText = "videoLib.aspx?videoId ="+videoList.ElementAt(3).VideoId;
            nodeList[38].InnerText = "./movieDetailedInfo.aspx?videoId=" + videArray1.ElementAt(3).VideoId;

            nodeList[49].InnerText = videArray1.ElementAt(4).VideoImage;
            //nodeList[50].InnerText = "videoLib.aspx?videoId ="+videoList.ElementAt(4).VideoId;
            nodeList[50].InnerText = "./movieDetailedInfo.aspx?videoId=" + videArray1.ElementAt(4).VideoId;
    
            xmlDoc.Save(HttpContext.Current.Server.MapPath("./global/homePage/dl_sample6.xml"));//保存。
        }


        public static void FillDetailsTable(Table table,int typeID)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
          
            List<Model.Video> videoList = videoDAL.GetVideosByTypeIdAndIsTry(typeID);
            int length = videoList.Count();
            Console.WriteLine("manager = " + length);
            Model.Video[] videArray = videoList.ToArray();
            Model.Video[] videArray1 = QuickSort(videArray, 0, length - 1);
            //table.Width = 568;
            //table.BorderStyle = BorderStyle.None;
            //table.HorizontalAlign = HorizontalAlign.Left;
            //table.CellSpacing = 10;
                 
            TableRow[] tr = new TableRow[2];
            TableCell tc = null;
            tr[0] = new TableRow();
            tr[1] = new TableRow();

            for (int j = 0; j < 4; j++)
           {
               tc = new TableCell();
               tc.Width = 235;
               tc.Height = 117;
               //tc.RowSpan = 7;
               tc.HorizontalAlign = HorizontalAlign.Center;
               tc.VerticalAlign = VerticalAlign.Middle;
               tc.Text = "<img src='" + videArray1.ElementAt(j).VideoImage + "' width='235' height='179' />";
               tr[0].Cells.Add(tc);

               tc = new TableCell();
               tc.Width = 235;
                    //tc.BackColor = "#F5E3D5";
                    //tc.HorizontalAlign = HorizontalAlign.Right;
               tc.Text = " <h1 align='left'class='STYLE11 STYLE41'>名称：<a href='./movieDetailedInfo.aspx?videoId=" + videArray1.ElementAt(j).VideoId + "'>" + videArray1.ElementAt(j).VideoName + "</a></h1>";
               tc.Text = tc.Text + "<h1 align='left' class='STYLE11 STYLE41'>导演：" + videArray1.ElementAt(j).VideoDirector + "</h1>";
               tc.Text = tc.Text + "<h1 align='left' class='STYLE11 STYLE41'>演员：" + videArray1.ElementAt(j).VideoActors + "</h1>";
               int hotScore = videArray1.ElementAt(j).VideohotScore;
               tc.Text = tc.Text + "<h1 align='left' class='STYLE11 STYLE41'>热度：";
               for (int m = 1; m <= hotScore; m++)
               {
                   tc.Text = tc.Text +" "+ "<img src='./images/img_01.jpg' width='10' height='10' align='middle' />";
               }
               tc.Text = tc.Text + "</h1>";
               tr[1].Cells.Add(tc);

               foreach (TableRow r in tr)
               {
                    table.Rows.Add(r);
               }               

            }
        }
        public static Model.Video[] QuickSort(Model.Video[] videArray, int low, int high)
        {
            //设定移动上下标
            int i = low;
            int j = high;
            System.Diagnostics.Debug.WriteLine(low +":::::::"+high);
            Model.Video middleVideo = videArray[(low + high) / 2];

            //直至分割出两个序列
            do
            {
                //扫描中值左边元素
                while (videArray[i].VideohotScore > middleVideo.VideohotScore && i < high)
                    i++;
                //扫描中值右边元素
                while (videArray[j].VideohotScore < middleVideo.VideohotScore && j > low)
                    j--;
                //找到了一对可交换的值
                if (i <= j)
                {
                    //交换 
                    Model.Video temp = videArray[i];
                    videArray[i] = videArray[j];
                    videArray[j] = temp;
                    i++;
                    j--;
                }
            } while (i <= j);
            //递归对比分割点元素都小的那个序列进行快速排序
            if (j > low)
                QuickSort(videArray, low, j);
            //递归对比分割点元素都大的那个序列进行快速排序
            if (i < high)
                QuickSort(videArray, i, high);
            return videArray;
        }
    }
}
